<?php
class Contact extends Controller
{
	function Contact()
	{
		parent::Controller();	
		$this->load->model('contact_model');
	}	
	function index()
	{
		$this->list_contact();		
	}
	//NHẬN DỮ LIỆU $_POST ĐẦU VÀO VÀ XỬ LÝ
	function build_data($data)
	{
		//$data_post['id'] = $data['id'];
		$data_post['position'] = $data['position'];
		$data_post['benefits'] = $data['benefits'];
		$data_post['job_description'] = $data['job_description'];
		$data_post['requirement'] = $data['requirement'];
		$data_post['bl_active'] = (isset($data['bl_active']))?1:0;
		$data_post['dt_create'] = date('y-m-d H:i:s');
		$data_post['records'] = $data['records'];		
		$data_post['filing_address'] = $data['filing_address'];
		$data_post['lang'] = strtolower($data['lang']);
		return $data_post;
	}
	function list_contact()
	{
		$data['title'] = lang('contact_title');
		$data['add'] = 'contact/add_contact';
		$data['num'] = $this->contact_model->get_num_contact();
		
		//PHÂN TRANG
		$config['base_url'] = base_url().'contact/list_contact/';
		$config['total_rows'] = $data['num'];
		$config['per_page'] =8;
		$config['uri_segment'] = 3;
		$config['first_link'] = '<<';
		$config['last_link'] = '>>';
		$config['next_link'] = '>';
		$config['prev_link'] = '<';
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		
		$data['list'] = $this->contact_model->get_all_contact($config['per_page'],(int)$this->uri->segment(3));
		
		$this->site_library->load('contact/list_contact',$data);
	}
	function add_careers()
	{	
		$data['title'] = 'them job';		
		
		//KIỂM TRA DỮ LIỆU NHẬP
		$this->form_validation->set_rules('position','Vị trí','trim|required');
		$this->form_validation->set_rules('benefits','Lợi ích','trim|required');
		$this->form_validation->set_rules('requirement','Yêu cầu','trim|required');
		$this->form_validation->set_rules('job_description','Mô tả công việc','trim|required');
		$this->form_validation->set_rules('records','Hồ sơ','trim|required');
		$this->form_validation->set_rules('filing_address','Nơi nộp hồ sơ','trim|required');		
		
		if($this->form_validation->run()== FALSE)
		{
		  $this->pre_message = validation_errors();
		}
		else
		{
		  $data = $this->build_data($_POST);
		  if($this->careers_model->save_careers($data))
		  {
			  $this->session->set_flashdata('message',lang('save_success'));
			  redirect('careers/list_careers');
		  }
		}
		$data['message'] = $this->pre_message;
		$this->_templates['page'] = 'careers/add_careers';
		$this->site_library->load($this->_templates['page'],$data);
	}
	function edit_contact()
	{	
		$data['contact'] = $this->contact_model->get_contact();
		$data['title'] = lang('update_address');		
		
		//KIỂM TRA DỮ LIỆU NHẬP
		$this->form_validation->set_rules('address','Địa chỉ','trim|required');
		$this->form_validation->set_rules('phone','Điện thoại','trim|required');
		$this->form_validation->set_rules('email','Email','trim|required');
		$this->form_validation->set_rules('website','Website','trim|required');
		$this->form_validation->set_rules('content','Nội dung','trim');
		$this->form_validation->set_rules('lang','Ngôn ngữ','trim|required');
		
		if($this->form_validation->run()== FALSE)
		{
		  $this->pre_message = validation_errors();
		}
		else
		{
		  $data = $this->build_data($_POST);
		  if($this->careers_model->save_careers($data))
		  {
			  $this->session->set_flashdata('message',lang('save_success'));
			  redirect('contact/list_contact');
		  }
		}
		$data['message'] = $this->pre_message;
		$this->_templates['page'] = 'contact/edit_contact';
		$this->site_library->load($this->_templates['page'],$data);
	}
	function del_careers()
	{
		$id = $this->uri->segment(3);
		$page = $this->uri->segment(4);
		if($this->careers_model->del_careers($id))
			$this->session->set_flashdata('message',lang('del_success'));
		else
			$this->session->set_flashdata('message',lang('del_fail'));
		redirect('careers/list_careers/'.$page);
	}
	function dels_careers()
	{
		if(!empty($_POST['ar_id']))
		{
			$ar_id = $this->input->post('ar_id');
			$page = $this->uri->segment(4);
			foreach($ar_id as $id)
			{
				if($this->careers_model->del_careers($id))
					$this->session->set_flashdata('message',lang('del_success'));
				else
					$this->session->set_flashdata('message',lang('del_fail'));
			}	
		}
		redirect('careers/list_careers/'.$page);
	}
}
?>